package cn.js.sip.rat.system.controller;

import cn.js.sip.rat.system.dto.AjaxResult;
import cn.js.sip.rat.system.dto.SysUserDto;
import cn.js.sip.rat.system.entity.SysUserEntity;
import cn.js.sip.rat.system.service.SysMenuService;
import cn.js.sip.rat.system.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: cn.js.sip.rat.system.controller
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2019-12-27   23:22
 */
@Controller
public class LoginController {
    private static Logger logger= LoggerFactory.getLogger(LoginController.class);
    @Resource(name = "sysMenuService")
    private SysMenuService sysMenuService;

    @PostMapping("login")
    @ResponseBody
    public AjaxResult login(@RequestBody SysUserDto sysUserDto) {
        UsernamePasswordToken token = new UsernamePasswordToken(sysUserDto.getUserName(), sysUserDto.getPassword());
//        token.setRememberMe(sysUserDto.getRememberMe().equals("true") ? true : false);
        token.setRememberMe(false);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            String sessionId = (String) subject.getSession().getId();
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("token",sessionId);
            return AjaxResult.success("登录成功",objectObjectHashMap);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        } catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
            return AjaxResult.error("未知错误，请联系管理员");
        }
    }
    @PostMapping("loginout")
    @ResponseBody
    public AjaxResult loginout() {
        ShiroUtils.logout();
        return AjaxResult.success();
    }

    @GetMapping("login")
    public String toLogin() {
        return "login";
    }


    // 系统首页
    @GetMapping("/index")
    public String index(Map<String, Object> map) {
        // 取身份信息
        SysUserEntity user = (SysUserEntity) ShiroUtils.getSession().getAttribute("user");
        // 根据用户id取出菜单
//        List<SysMenuDto> menus = sysMenuService.getMenusByUser(user);
//        /*List<SysMenuEntity> menus = menuService.selectMenusByUser(user);
//        mmap.put("menus", menus);
//
//        mmap.put("copyrightYear", Global.getCopyrightYear());
//        mmap.put("demoEnabled", Global.isDemoEnabled());*/
//        map.put("menus", menus);
//        map.put("user", user);
        return "index";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap) {
        mmap.put("version", 1.0);
        return "main";
    }




    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap) {
        return "skin";
    }
}
